package kr.co.sdsway2.web.client;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.sdsway2.comm.SdsWay2Util;
import kr.co.sdsway2.service.RemoconService;
import kr.co.sdsway2.vo.AvatarVO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RemoconController3 {
protected final Log logger = LogFactory.getLog(getClass());
	
	@RequestMapping("/remocon3")
	public String remocon3(Model model,HttpServletResponse response, HttpServletRequest request){
		
		logger.info("┌──────────────────────────────────────Returning hello view───────────────────────────────────────────────┐");
		SdsWay2Util.printReq(request,logger);
		logger.info("└──────────────────────────────────────Returning hello view───────────────────────────────────────────────┘");
		
//		String avatarId = req.getParameter("avatarId");
		String avatarId = "01085177411";
		String direction =	request.getParameter("direction");
		String distance = request.getParameter("distance");
			
		RemoconService service = new RemoconService();
//		AvatarVO avatarVO = dao.selectAvatarByPk(avatarId);
		
		if(	direction!=null && !"".equals(direction) && 
			distance!=null && !"".equals(distance) &&
			service.isAvatarExist(avatarId)){
			service.updatePosition(avatarId,direction,distance);
		}
		
		AvatarVO avatarVO = service.getAvatarPosition(avatarId);
		request.setAttribute("avatarVO", avatarVO);
		
		ModelAndView mav = new ModelAndView("/WEB-INF/jsp/remocon.jsp");
        mav.addObject("avatarVO", avatarVO);
        
//		return "/WEB-INF/jsp/remocon.jsp";
        return "remocon.jsp";
	}
	
	@RequestMapping("/remoconAjax")
	public void remoconAjax(Model model, HttpServletRequest req){
		System.out.println("┌──────────────────────────────────────remoconAjax───────────────────────────────────────────────┐");
		SdsWay2Util.printReq(req,logger);
		System.out.println("└──────────────────────────────────────remoconAjax───────────────────────────────────────────────┘");
		
//		String avatarId = req.getParameter("avatarId");
		String avatarId = "01085177411";
		String direction =	req.getParameter("direction");
		String distance = req.getParameter("distance");
			
		RemoconService service = new RemoconService();
//		AvatarVO avatarVO = dao.selectAvatarByPk(avatarId);
		
		if(	direction!=null && !"".equals(direction) && 
			distance!=null && !"".equals(distance) &&
			service.isAvatarExist(avatarId)){
			service.updatePosition(avatarId,direction,distance);
		}
		
		AvatarVO avatarVO = service.getAvatarPosition(avatarId);
//		Map avatarVOMap = new HashMap();
//		avatarVOMap.put("avatarId", avatarVO.getAvatarId());
//		avatarVOMap.put("leftPosition", avatarVO.getLeftPosition());
//		avatarVOMap.put("rightPosition", avatarVO.getRightPosition());
//		logger.debug("avatarVOMap : "+avatarVOMap);
//		ModelAndView mav = new ModelAndView("jsonView","avatarVO",avatarVOMap);
//		ModelAndView mav = new ModelAndView("jsonView");
		model.addAttribute("avatarVO", avatarVO);
//		req.setAttribute("avatarVO", avatarVO);
//      mav.addObject("avatarVO", avatarVO);
	}	
	
}
